package com.dd.ljzapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.dd.ljzapplication.R;

import java.util.ArrayList;

/**
 * Created by LiJZ on 2016/10/18.
 */
public class HXPictureAdapter extends RecyclerView.Adapter {
	private ArrayList<String> listData;

	private Context mContext;

	/**
	 * 定义一个接口，处理recyclerview的item的点击事件
	 */
	public interface OnRVItemClickListener {
		/**
		 * @param v        该条目的view对象
		 * @param position 该位置对应的position
		 */
		void setItemClickListener(View v, int position);
	}

	/**
	 * 定义一个接口，处理recyclerview的item的长按事件
	 */
	public interface OnRVItemLongClickListener {
		/**
		 * @param v        该条目的view对象
		 * @param position 该位置对应的position
		 */
		void setItemLongClickListener(View v, int position);
	}



	/**
	 * item点击事件的接口
	 */
	private OnRVItemClickListener mRVItemClickListener;
	/**
	 * item长按事件的接口
	 */
	private OnRVItemLongClickListener mRVItemLongClickListener;

	//对外暴露的接口
	public void setOnRVItemClickListener(OnRVItemClickListener mRVItemClickListener) {
		this.mRVItemClickListener = mRVItemClickListener;
	}

	//对外暴露的接口
	public void setOnRVItemLongClickListener(OnRVItemLongClickListener rvItemLongClickListener) {
		this.mRVItemLongClickListener = rvItemLongClickListener;
	}


	public HXPictureAdapter(Context context) {
		this.mContext = context;
	}

	public void setDate(ArrayList<String> listData) {
		this.listData = listData;
		notifyDataSetChanged();
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(mContext).inflate(R.layout.item_hx_camera, parent, false);
		MyViewHolder myViewHolder = new MyViewHolder(view);
		return myViewHolder;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
		MyViewHolder myViewHolder = (MyViewHolder) holder;
		String path = listData.get(position);


//		Bitmap bm = BitmapFactory.decodeFile(listData.get(position));
		Glide.with(mContext).load(listData.get(position)).into(myViewHolder.iv);
//		myViewHolder.iv.setImageBitmap(bm);
		myViewHolder.iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mRVItemClickListener.setItemClickListener(v, position);
			}
		});

		myViewHolder.iv.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				mRVItemLongClickListener.setItemLongClickListener(v,position);
				return true;
			}
		});

//		myViewHolder.tv.setText(listData.get(position));
//		myViewHolder.tv.setOnClickListener(new View.OnClickListener() {
//			@Override
//			@Override
//			public void onClick(View v) {
//				mRVItemClickListener.setItemClickListener(v, position);
//			}
//		});
	}

	@Override
	public int getItemCount() {
		return listData == null || listData.size() == 0 ? 0 : listData.size();
	}


	static class MyViewHolder extends RecyclerView.ViewHolder {
		ImageView iv;
		TextView tv;
		VideoView vv;

		public MyViewHolder(View itemView) {
			super(itemView);
			iv = (ImageView) itemView.findViewById(R.id.iv);
			tv = (TextView) itemView.findViewById(R.id.tv);
			vv = (VideoView) itemView.findViewById(R.id.vv);
		}
	}
}
